package com.cloud.annex;

import com.alibaba.fastjson.JSONObject;
import com.cloud.common.constants.GlobalConstants;
import com.cloud.domain.LoginUser;
import com.cloud.enums.LoginDevice;
import com.cloud.utils.SecurityUtils;
import com.cloud.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 附件地址转化
 *
 * @author yzj
 */

@Component
public class AnnexUrlService {

    @Autowired
    private AnnexConfiguration annexConfiguration;

    /**
     * 获取图片转化路径
     *
     * @param url 源路径
     * @return
     */
    public String getImageTurnPath(String url) {
        return getImageOrAnnexInvertPath(url, getVisitUrl());
    }


    /**
     * 获取视频转化路径
     *
     * @param url 源路径
     * @return
     */
    public String getVideoTurnPath(String url) {
        return getImageOrVideoInvertPath(url, getVisitUrl());
    }

    /**
     * 获取附件转化路径
     *
     * @param url
     * @return
     */
    public String getAnnexTurnPath(String url) {
        return getImageOrAnnexInvertPath(url, getVisitUrl());
    }

    /**
     * 获取图片或者视频转化路径
     *
     * @param url 源路径
     * @return
     */
    public String getImageOrVideoPath(String url, String deviceType) {
        if (LoginDevice.PC.getCode().equals(deviceType)) {
            return getImageOrVideoInvertPath(url, annexConfiguration.getVisitComputerUrl());
        } else if (LoginDevice.APP.getCode().equals(deviceType)) {
            return getImageOrVideoInvertPath(url, annexConfiguration.getVisitMobileUrl());
        } else {
            return getImageOrVideoInvertPath(url, annexConfiguration.getVisitDefaultUrl());
        }
    }

    /**
     * 获取附件访问地址
     *
     * @return
     */
    private String getVisitUrl() {
        try {
            // 根据登录账户设备信息匹配附件访问地址
            LoginUser userInfo = SecurityUtils.getLoginUser();
            if (StringUtils.isNotNull(userInfo)) {
                if (LoginDevice.PC.getCode().equals(userInfo.getLoginDevice())) {
                    return annexConfiguration.getVisitComputerUrl();
                }
                if (LoginDevice.APP.getCode().equals(userInfo.getLoginDevice())) {
                    return annexConfiguration.getVisitMobileUrl();
                }
            } else {
                // 项目初始化时,赋值默认访问地址
                return annexConfiguration.getVisitDefaultUrl();
            }
        } catch (Exception e) {
            return annexConfiguration.getVisitDefaultUrl();
        }
        return null;
    }

    /**
     * 获取图片和视频转换后的附件访问地址
     * 根据存储桶名称替换访问地址
     *
     * @param url      图片或视频原路径
     * @param visitUrl 配置中访问路径
     * @return
     */
    private String getImageOrVideoInvertPath(String url, String visitUrl) {
        try {
            if (StringUtils.isNotNull(url)) {
                List<String> nowUrlList = new ArrayList<>();
                String[] urlList = url.split(",");
                if (urlList.length > 0) {
                    for (int i = 0; i < urlList.length; i++) {
                        String storeUrl = null;
                        if (url.contains(GlobalConstants.annexNodeName)) {
                            storeUrl = urlList[i].substring(0, urlList[i].indexOf(GlobalConstants.annexNodeName));
                            storeUrl = visitUrl + urlList[i].substring(storeUrl.length(), urlList[i].length());
                        } else if (url.contains(GlobalConstants.iconNodeName)) {
                            storeUrl = urlList[i].substring(0, urlList[i].indexOf(GlobalConstants.iconNodeName));
                            storeUrl = visitUrl + urlList[i].substring(storeUrl.length(), urlList[i].length());
                        } else {
                            storeUrl = urlList[i];
                        }
                        nowUrlList.add(storeUrl);
                    }
                    return String.join(",", nowUrlList);
                }
            }
        } catch (Exception e) {
            return url;
        }
        return url;
    }

    /**
     * 获取附件转换后的附件访问地址
     * 根据存储桶名称替换访问地址
     *
     * @param url      附件原路径
     * @param visitUrl 配置中访问路径
     * @return
     */
    private String getImageOrAnnexInvertPath(String url, String visitUrl) {
        try {
            if (StringUtils.isNotNull(url)) {
                List<String> urlList = JSONObject.parseArray(url, String.class);
                if (StringUtils.isNotNull(urlList)) {
                    List<String> nowUrlList = new ArrayList<>();
                    for (String item : urlList) {
                        String storeUrl = null;
                        JSONObject jsonObject = JSONObject.parseObject(item);
                        String realUrl = jsonObject.getString("url");
                        if (url.contains(GlobalConstants.annexNodeName)) {
                            storeUrl = visitUrl + realUrl.substring(realUrl.indexOf(GlobalConstants.annexNodeName));
                            jsonObject.put("url", storeUrl);
                            nowUrlList.add(jsonObject.toString());
                        }
                        if (url.contains(GlobalConstants.iconNodeName)) {
                            storeUrl = visitUrl + realUrl.substring(realUrl.indexOf(GlobalConstants.iconNodeName));
                            jsonObject.put("url", storeUrl);
                            nowUrlList.add(jsonObject.toString());
                        }
                    }
                    return String.valueOf(nowUrlList);
                }
            }
        } catch (Exception e) {
            return url;
        }
        return url;
    }

}
